package com.badibadi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils.TruncateAt;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

import com.badibadi.activity.ActivityOverViewActivity;
import com.badibadi.activity.CalendarActivity;
import com.badibadi.activity.ClubListActivity;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.infos.Results;
import com.badibadi.infos.listType_Model;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.TempTools;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.libjph.util.StringUtil;

public class IndexClub1Fragment extends BaseFragment {// index_btn
													// Constants.END_TIME

	private GridView gv;
	private IndexGridViewAdapter indexGridViewadapter;
	private Button index_creationclub;// 创建俱乐部按钮
	private MySharePreferences preferences;
	private int languageType = 0;
	private List<ClueTypeModel> clueTypeModels;
	private listType_Model listType;
	private TextView startTime, endTime;
	/**
	 * 存储颜色
	 */
	private int[] color_id;
	private CalendarTools calendarTools;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(getActivity());
					// Utils.showMessage(getActivity(), "网络连接出错");
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				try {
					Utils.ExitPrgress(getActivity());

					indexGridViewadapter = new IndexGridViewAdapter(
							getActivity());
					gv.setAdapter(indexGridViewadapter);
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 3:
				try {
					Utils.ExitPrgress(getActivity());
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		color_id = new int[] {
				getActivity().getResources().getColor(R.color.orange_fa7822),
				getActivity().getResources().getColor(R.color.blue),
				getActivity().getResources().getColor(R.color.black_45597a),
				getActivity().getResources().getColor(R.color.black_0) };
		calendarTools = CalendarTools.getInstance();
	}

	private void Show_type_activity() {
		Utils.showPrgress(getActivity());
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", "huodong");
				map.put("languageType", TempTools.BackLanguage(languageType));
				String acticontent = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "type/listType");
				if (acticontent != null) {
					Results results = Utils.checkResult_NNN(getActivity(),
							acticontent);
					if (results != null && results.getRetmsg() != null) {
//						   ("结果是:" + results.getRetmsg());
						try {
							listType = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), listType_Model.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {

					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		final View view = inflater.inflate(R.layout.fragment_index_club1, null);
		preferences = new MySharePreferences(getActivity(), "language");
		languageType = preferences.get("language", 0);
		listType = new listType_Model();
		startTime = (TextView) view.findViewById(R.id.startTime);
		endTime = (TextView) view.findViewById(R.id.endTime);
		gv = (GridView) view.findViewById(R.id.index_club_gridview0);
		Constants.content_or_time = false;
		//    
		// if (Constants.content_or_time) {// 当是时间搜索模式的时候才能进入此判断
		// if (StringUtil.isNullOrEmpty(startTime.getText().toString().trim())
		// && StringUtil.isNullOrEmpty(endTime.getText().toString()
		// .trim())) {
		// Constants.content_or_time = true;
		// } else {
		// Constants.content_or_time = false;
		// }
		// }
		//    
		// gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View view,
		// int position, long arg3) {
		// // TODO Auto-generated method stub
		// // Toast.makeText(getActivity(),
		// // "选择了" + clueTypeModels.get(position).getName(), 0)
		// // .show();
		// Intent intent = new Intent(getActivity(),
		// CalendarActivity.class);
		// intent.putExtra("CalendarActivity",
		// clueTypeModels.get(position));
		// startActivity(intent);
		//
		// }
		// });
		Show_type_activity();
		// ceshi(1);
		view.findViewById(R.id.StartTime).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						TextView t = (TextView) view
								.findViewById(R.id.startTime);
						calendarTools.createDateDialog_1(getActivity(), t,
								"start");
					}
				});
		view.findViewById(R.id.EndTime).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						TextView t = (TextView) view.findViewById(R.id.endTime);
						calendarTools.createDateDialog_1(getActivity(), t,
								"end");
					}
				});
		return view;
	}

	public class IndexGridViewAdapter extends BaseAdapter {

		private Context context;

		// private String[] project;
		// private MyClubInfos myClubInfos;

		public IndexGridViewAdapter(Context ct) {
			context = ct;
			// project = context.getResources().getStringArray(
			// R.array.activity_type);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listType.getLists().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listType.getLists().get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			Button btn = new Button(context);
			btn.setText(listType.getLists().get(position).getName());
			btn.setClickable(false);
			btn.setFocusable(false);
			btn.setTextColor(color_id[position % color_id.length]);//
			
			btn.setSingleLine(true);
			btn.setMaxEms(8);
			btn.setEllipsize(TruncateAt.END);
			btn.setBackgroundResource(R.drawable.shape_graye5e5e5_circular_bead);
			btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(getActivity(),
							ActivityOverViewActivity.class);
					// intent.putExtra("CalendarActivity",
					// listType.getLists().get(position));
					// startActivity(intent);

					// Intent intent = new Intent(getActivity(),
					// ClubListActivity.class);sdf
					// sdf
					clueTypeModels = new ArrayList<ClueTypeModel>();
					for (int i = 0; i < listType.getLists().size(); i++) {
						ClueTypeModel cc = new ClueTypeModel();
						cc.setId(listType.getLists().get(i).getId());
						cc.setName(listType.getLists().get(i).getName());
						clueTypeModels.add(cc);
					}
					intent.putExtra("CalendarActivity",
							clueTypeModels.get(position));
					startActivity(intent);
				}
			});
			return btn;
		}
	}

}
